package com.kk.springboot_002.controller;

import com.github.pagehelper.PageInfo;
import com.kk.springboot_002.pojo.Fruit;
import com.kk.springboot_002.pojo.dto.FruitDto;
import com.kk.springboot_002.pojo.dto.RegisterDto;
import com.kk.springboot_002.service.FruitService;
import com.kk.springboot_002.service.UserService;

import com.kk.springboot_002.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private FruitService fruitService;

    /**
     * 更新用户信息
     * @param updateDto
     * @param authentication
     * @return
     */
    @RequestMapping("/update")
    public Response<String> updateUser(@RequestBody RegisterDto updateDto, Authentication authentication) {
        try {
            String username = authentication.getName();
            userService.updateUser(username,updateDto);
            return Response.success("更新成功");
        } catch (Exception e) {
            return Response.error(500, e.getMessage());
        }
    }

    /**
     * 添加水果
     * @param fruitDto
     * @return
     */
    @PostMapping("/addFruit")
    public Response<String> addFruit(@RequestBody FruitDto fruitDto){
        if (fruitDto.getFruitName() == null || fruitDto.getFruitName().isEmpty()) {
            return Response.error(500, "水果名称不能为空");
        }
        // 从SecurityContext中获取当前登录用户的用户名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        try {
            fruitService.addFruit(username, fruitDto);
            return Response.success("添加成功");
        } catch (Exception e) {
            return Response.error(500, e.getMessage());
        }
    }

    /**
     * 获取水果列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/fruits")
    public Response<PageInfo<Fruit>> getFruits(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();

        PageInfo<Fruit> fruits = fruitService.getFruits(username, page, size);
        return Response.success(fruits);
    }

    /**
     * 注销用户
     * @return
     */
    @DeleteMapping("/delete")
    public Response<String> delete(){
        // 从Spring security上下文中获取当前用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        // 获取当前用户名
        String username = authentication.getName();
        // 删除用户
        boolean result = userService.deleteUser(username);
        if(result){
            return Response.success("注销成功",null);
        } else {
            return Response.error(500, "注销失败");
        }
    }
}
